<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.SortedSet"%>
<%@page import="java.util.TreeSet"%>
<%@page import="java.util.Iterator"%>
<%@page import="model.User"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Users</title>
</head>
<body>
<a href="/hmg/IndexServlet">Home</a>
<br/>
<%
SortedSet<User> u = (TreeSet<User>)request.getAttribute("users");
if(u != null) {
  if(u.isEmpty()){
    %>
    No users where found!
    <%
  } else {
	  Iterator<User> i = u.iterator();
	  %>
	  <h2>List of users in the database:</h2>
	  <table id="users_list">
	  	<thead>
	  		<tr>
	  			<th>Name</th>
	  			<th>Mail</th>
	  		</tr>
	  	</thead>
	  <%
	  while(i.hasNext()){
	    User next = i.next();
	    %>
	    <tr>
	    	<td><%= next.getName() %></td>
	    	<td><%= next.getMail() %></td>
	    </tr>
	    <%
	  }
	  %>
	  </table>
	  <%
  }
} else {
  %>
  	<h2>No users where found!</h2>
  <%
}
%>

<h2>New user?</h2>
<%
String m, n, addedname;

m = (String)request.getAttribute("mail");
if(m != null){
  m = "value='" + m + "' ";
} else {
  m = "";
}

n = (String)request.getAttribute("name");
if(n != null){
  n = "value='" + n + "'"; 
} else {
  n = "";
}

addedname = (String)request.getAttribute("addedname");
if(addedname != null){
  n = addedname;
}
%>
<form method="POST" action="/hmg/UserServlet">
<table>
	<tr>
		<td>Name:</td>
		<td><input type="text" name="name" id="name" <%= n %>/></td>
	</tr>
	<tr>
		<td>Mail:</td>
		<td><input type="text" name="mail" id="mail" <%= m %>/></td>
	</tr>
	<tr>
		<td>Password:</td>
		<td><input type="password" name="pass1" id="pass1"/></td>
	</tr>
	<tr>
		<td>Confirm Password:</td>
		<td><input type="password" name="pass2" id="pass2"/></td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" name="btn_adduser" id="btn_adduser" value="Add user"/></td>
	</tr>
</table>
</form>
<%
Boolean added = (Boolean)request.getAttribute("added");
if(added!=null){
	if(added) {
	  %> <h3> User added! </h3> <%
	} else {
	  	String error = (String)request.getAttribute("error");
	  	if(error != null) {
			%> 
			<h3> <%= error %> </h3>
			<%
	  	} else {
	  	  	%> 
	  		<h3> User could not be added! </h3>
	  		<%
	  	}
	}
}
%>
</body>
</html>